package com.comdo.newup_loan.module.home;

import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.model.Result;
import com.comdo.newup_loan.model.WanLoanBean;
import com.comdo.newup_loan.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by liyuan on 2017/11/2.
 */

public class WantLoanPresenter extends WantLoanContract.Presenter{
    @Override
    public void sumbit(int userId, String name, String mobile) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("name", name);
        map.put("mobile", mobile);
        map.put("token", Config.loginInfo.getToken());
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().loanIntent(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<WanLoanBean>>() {
                    @Override
                    public void accept(Result<WanLoanBean> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.success("提交成功!");
                        } else {
                            mView.getFail(Result.getMessage());

                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.dismissLoadingDialog();
                        mView.getFail("请求失败");
                    }
                }));
    }
}
